package com.epro.dx.util;

import android.content.Context;

import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.factory.ProviderFactory;
import com.weipu.common.facade.impl.ProductServiceProviderImpl;
import com.weipu.common.facade.model.CatelogModel;
import com.weipu.common.facade.model.OneKeyFilterResponseModel;
import com.weipu.common.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/************************
 * @author Tony
 * @Date:2016-4-5下午3:27:12
 * @描述:一键过滤本地化工具集
 ************************/
public class OnekeyFilterLocalUtil {

    /**
     * @param list
     * @param isShow
     * @param mContext
     * @创建：Tony 2016-4-5下午3:28:57
     * @描述：根据黑白名单保存到本地数据库
     */
    public static void saveToLocal(Context mContext, long customerID, List<CatelogModel> list, boolean isShow) {
        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                CatelogModel model = list.get(i);
                model.setShow(isShow);
                model.setCustomerID(customerID);
                ProviderFactory.createOnekeyFilterService().save(mContext, model);
            }
        }
    }

    /**
     * @param mContext
     * @param customerID
     * @创建：Tony 2016-4-5下午3:56:49
     * @描述：清空本地存储的信息
     */
    public static void deleteLocalData(Context mContext, long customerID) {
        //先查询是否有数据
        ArrayList<CatelogModel> listByCustomerID = ProviderFactory.createOnekeyFilterService().getListByCustomerID(mContext, customerID);
        if (null != listByCustomerID) {
            //不为空，执行清除逻辑
            ProviderFactory.createOnekeyFilterService().deleteByCustomerID(mContext, customerID);
        }
    }

    /**
     * @param mContext
     * @param customerID
     * @return
     * @创建：Tony 2016-4-5下午4:17:44
     * @描述：获取本地存储的黑白名单信息
     */
    public static OneKeyFilterResponseModel getLocalData(Context mContext, long customerID) {
        OneKeyFilterResponseModel model = null;
        ArrayList<CatelogModel> listByCustomerID = ProviderFactory.createOnekeyFilterService().getListByCustomerID(mContext, customerID);
        if (null != listByCustomerID) {
            model = new OneKeyFilterResponseModel();
            ArrayList<CatelogModel> whiteList = new ArrayList<CatelogModel>();
            ArrayList<CatelogModel> blackList = new ArrayList<CatelogModel>();
            for (int i = 0; i < listByCustomerID.size(); i++) {
                CatelogModel catelogModel = listByCustomerID.get(i);
                if (catelogModel.isShow()) {
                    whiteList.add(catelogModel);
                } else {
                    blackList.add(catelogModel);
                }
            }
            if (null != whiteList && whiteList.size() > 0) {
                model.setWhiteCategoryData(whiteList);
            }
            if (null != blackList && blackList.size() > 0) {
                model.setBlackCategoryData(blackList);
            }
        }
        return model;
    }

    /**
     * @return
     * @创建：Tony 2016-4-6下午2:10:32
     * @描述：当没有登录状态下，本地数据库没有黑白名单信息时，调用此方法将一级目录全部转为白名单
     */
    public static List<CatelogModel> turnTopCategorysToWhiteCategorys(Context mContext) {
        ArrayList<CatelogModel> whiteCategorys = ProviderFactory.createCategoryServiceDBProvider().queryCategoryListByParentCategoryID(ProductServiceProviderImpl.TOP_CATEGORY_DEFAULT_PARENT_ID);
        //保存到本地数据库
        if (whiteCategorys != null && whiteCategorys.size() > 0) {
            for (int i = 0; i < whiteCategorys.size(); i++) {
                CatelogModel catelogModel = whiteCategorys.get(i);
                catelogModel.setShow(true);
                catelogModel.setCustomerID(-1);
                ProviderFactory.createOnekeyFilterService().save(mContext, catelogModel);
            }
        }
        return whiteCategorys;
    }

    /**
     * @param mContext
     * @return
     * @创建：Tony 2016-4-7下午6:01:43
     * @描述：获取用户本地存储的白名单id，用于访问产品接口传參
     */
    public static String getLocalCategoryIds(Context mContext) {
        StringBuilder sb = new StringBuilder();
        OneKeyFilterResponseModel localData = getLocalData(mContext, ActivityUtil.isLogin(mContext) ? ActivityUtil.getCustomerID() : -1);
        if (null != localData) {
            List<CatelogModel> whiteCategoryData = localData.getWhiteCategoryData();
            if (null != whiteCategoryData && whiteCategoryData.size() > 0) {
                for (int i = 0; i < whiteCategoryData.size(); i++) {
                    if (i == whiteCategoryData.size() - 1) {
                        sb.append(whiteCategoryData.get(i).getBrowseID() + "");
                    } else {
                        sb.append(whiteCategoryData.get(i).getBrowseID() + "-");
                    }
                }
            }
        } else {
            //本地没有设置过，取所有一级品类为白名单
            List<CatelogModel> turnTopCategorysToWhiteCategorys = turnTopCategorysToWhiteCategorys(mContext);
            if (null != turnTopCategorysToWhiteCategorys && turnTopCategorysToWhiteCategorys.size() > 0) {
                for (int i = 0; i < turnTopCategorysToWhiteCategorys.size(); i++) {
                    if (i == turnTopCategorysToWhiteCategorys.size() - 1) {
                        sb.append(turnTopCategorysToWhiteCategorys.get(i).getBrowseID() + "");
                    } else {
                        sb.append(turnTopCategorysToWhiteCategorys.get(i).getBrowseID() + "-");
                    }
                }
            }
        }
        return sb.toString();
    }

    /**
     * @param mContext
     * @return
     * @创建：Tony 2016-4-12下午1:31:45
     * @描述：根据登录状态获取是否开启过滤
     */
    public static boolean getFilterState(Context mContext) {
        boolean isFilter;
        if (ActivityUtil.isLogin(mContext)) {
            isFilter = "true".equals(StringUtil.getConfig(ActivityUtil.getCustomerID() + ServerConstant.OneKeyFilter.ISFILTERKEY_ONLINE, "false"));
        } else {
            isFilter = "true".equals(StringUtil.getConfig(ServerConstant.OneKeyFilter.ISFILTERKEY_LOCAL, "false"));
        }
        return isFilter;
    }

    /***
     * 根据cagegory的分类，获取id
     * @return
     */
    public static String getCategoryIds() {
        ArrayList<CatelogModel> categoryList = ProviderFactory.createCategoryServiceDBProvider().queryCategoryListByParentCategoryID(ProductServiceProviderImpl.TOP_CATEGORY_DEFAULT_PARENT_ID);

        StringBuffer sb = new StringBuffer();
        for (CatelogModel catelogModel : categoryList) {
            if (sb.length() > 0) {
                sb.append("-");
            }
            sb.append(catelogModel.getBrowseID());
        }
        return sb.toString();
    }
}
